#include "GraphicsLibrary.h"

#define PI 3.1415926

void Transform2::SetMatrix(Point2_H *P, int ptNumber)
{
	this->P = P;
	this->ptNumber = ptNumber;
}

void Transform2::Identity(void)
{
	T[0][0] = 1.0; T[0][1] = 0.0; T[0][2] = 0.0;
	T[1][0] = 0.0; T[1][1] = 1.0; T[1][2] = 0.0;
	T[2][0] = 0.0; T[2][1] = 0.0; T[2][2] = 1.0;
}

void Transform2::Translate(double tx, double ty)
{
	Identity();
	T[0][2] = tx;
	T[1][2] = ty;
	MultiplyMatrix();
}

void Transform2::Scale(double sx, double sy)
{
	Identity();
	T[0][0] = sx;
	T[1][1] = sy;
	MultiplyMatrix();
}

void Transform2::Scale(double sx, double sy, Point2_H p)
{
	Translate(-p.X, -p.Y);
	Scale(sx, sy);
	Translate(p.X, p.Y);
}

void Transform2::Rotate(double beta)
{
	Identity();
	T[0][0] = cos(beta*PI / 180); T[0][1] = -sin(beta*PI / 180);
	T[1][0] = sin(beta*PI / 180); T[1][1] = cos(beta*PI / 180);
	MultiplyMatrix();
}

void Transform2::Rotate(double beta, Point2_H p)
{
	Translate(-p.X, -p.Y);
	Rotate(beta);
	Translate(p.X, p.Y);
}

void Transform2::ReflectOrg(void)
{
	Identity();
	T[0][0] = -1;
	T[1][1] = -1;
	MultiplyMatrix();
}

void Transform2::ReflectX(void)
{
	Identity();
	T[0][0] = 1;
	T[1][1] = -1;
	MultiplyMatrix();
}

void Transform2::ReflectY(void)
{
	Identity();
	T[0][0] = -1;
	T[1][1] = 1;
	MultiplyMatrix();
}

void Transform2::Shear(double b, double c)
{
	Identity();
	T[0][1] = b;
	T[1][0] = c;
	MultiplyMatrix();
}

void Transform2::MultiplyMatrix(void)
{
	Point2_H *PTemp = new Point2_H[ptNumber];

	for (int i = 0; i < ptNumber; ++i)
	{
		PTemp[i] = P[i];
	}
	for (int i = 0; i < ptNumber; ++i)
	{
		P[i].X = T[0][0] * PTemp[i].X + T[0][1] * PTemp[i].Y + T[0][2] * PTemp[i].W;
		P[i].Y = T[1][0] * PTemp[i].X + T[1][1] * PTemp[i].Y + T[1][2] * PTemp[i].W;
		P[i].W = T[2][0] * PTemp[i].X + T[2][1] * PTemp[i].Y + T[2][2] * PTemp[i].W;
	}

	delete[]PTemp;
}